// pages/home/home.js
const app = getApp()
var QR = require("../../utils/qrcode.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    casArray: ['贵阳云卡', '遵义云卡', '都匀云卡', '兴义云卡', '贵安云卡'],
    casIndex: 0,
    city:'请选择城市',
    isOpencrad:true,
    isQrcoed: false,
    placeholder:"https://www.baidu.com/",
    imagePath: '',
    canvasHidden: false,
    maskHidden: true,
    cityDefault:"贵阳云卡",
    balance:"58.00"

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var size = this.setCanvasSize();//动态设置画布大小
    var initUrl = this.data.placeholder;
    this.createQrCode(initUrl, "mycanvas", size.w, size.h);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  buttonClick: function(){
   console.log("被点击了啦啦啦啦啦啦......")
  },

  bindCasPickerChange: function (e) {
      console.log('乔丹选的是', this.data.casArray[e.detail.value])
      if (e.detail.value == 4) {
         this.setData({ reply: true })
      } else {
         this.setData({ reply: false })
      }
      this.setData({
         casIndex: e.detail.value,
         city: this.data.casArray[e.detail.value]

      })

   },
  addCard: function (e) {
    app.globalData.addcard = this.data.casArray[e.detail.value];
    console.log("新增云卡......" + app.globalData.addcard)
    wx.navigateTo({    //保留当前页面，跳转到应用内的某个页面（最多打开5个页面，之后按钮就没有响应的）
      url: "/pages/login/login"
    })
  }
  ,
  changeCard: function (e) {
    app.globalData.addcard = this.data.casArray[e.detail.value];
    console.log("切换云卡......" + app.globalData.addcard)
  },
  createQrCode: function (url, canvasId, cavW, cavH) {
    //调用插件中的draw方法，绘制二维码图片
    QR.api.draw(url, canvasId, cavW, cavH);
    setTimeout(() => { this.canvasToTempImage(); }, 1000);

  }, canvasToTempImage: function () {
    var that = this;
    wx.canvasToTempFilePath({
      canvasId: 'mycanvas',
      success: function (res) {
        var tempFilePath = res.tempFilePath;
        console.log(tempFilePath);
        that.setData({
          imagePath: tempFilePath,
          // canvasHidden:true
        });
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  
  //适配不同屏幕大小的canvas
  setCanvasSize:function () {
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / 686;//不同屏幕下canvas的适配比例；设计稿是750宽
      var width = res.windowWidth / scale;
      var height = width;//canvas画布为正方形
      size.w = width;
      size.h = height;
    } catch (e) {
      // Do something when catch error
      console.log("获取设备信息失败" + e);
    }
    return size;
  },
  formSubmit: function (e) {
    var that = this;
    var url = e.detail.value.url;
    that.setData({
      maskHidden: false,
    });
    wx.showToast({
      title: '生成中...',
      icon: 'loading',
      duration: 2000
    });
    var st = setTimeout(function () {
      wx.hideToast()
      var size = that.setCanvasSize();
      //绘制二维码
      that.createQrCode(url, "mycanvas", size.w, size.h);
      that.setData({
        maskHidden: true
      });
      clearTimeout(st);
    }, 2000)

  }

})